#include "bsp_magnetic.h"
#include "bsp_param.h"
#include "bsp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"

SemaphoreHandle_t xSemaphore_AC;

void magnetic_init()
{
    /* 输出接口 */
    gpio_config_t io_conf1 = {
        .intr_type = GPIO_INTR_DISABLE,
        .mode =  GPIO_MODE_DEF_OUTPUT,
        .pin_bit_mask = GPIO_OUTPUT_EN_PIN_SEL,
        .pull_down_en = 1,                    
        .pull_up_en = 0,
    };
    gpio_config(&io_conf1);

    gpio_config_t io_conf2 = {
        .intr_type = GPIO_INTR_DISABLE,
        .mode =  GPIO_MODE_DEF_OUTPUT,
        .pin_bit_mask = GPIO_OUTPUT_PIN_SEL,
        .pull_down_en = 1,                    
        .pull_up_en = 0,
    };
    gpio_config(&io_conf2);
    /* A电机输出pwm */
    ledc_timer_config_t A_pwm_timer = {
        .speed_mode       = LEDC_LOW_SPEED_MODE,
        .duty_resolution  = LEDC_TIMER_8_BIT,
        .timer_num        = LEDC_TIMER_0,
        .freq_hz          = A_PWM_FREQUENCY,  // Set output frequency at 4 kHz
        .clk_cfg          = LEDC_AUTO_CLK
    };

    ESP_ERROR_CHECK(ledc_timer_config(&A_pwm_timer));
    // Prepare and then apply the LEDC PWM channel configuration
    ledc_channel_config_t A_pwm_channel = {
        .speed_mode     = LEDC_LOW_SPEED_MODE,
        .channel        = A_PWM_CHANNEL,
        .timer_sel      = LEDC_TIMER_0,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = A_PWM_PORT,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&A_pwm_channel));

    xSemaphore_AC = xSemaphoreCreateBinary();
}


static void motor_speed_pwm_set(uint8_t pwm_channel, uint16_t duty)
{
    ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, pwm_channel, duty));
    ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, pwm_channel));
}



void magnetic_output(uint8_t mode,uint8_t power_value)
{
    xSemaphoreGive(xSemaphore_AC);
    mygptimer2_start();
    if(mode == MODE_AC){
        if(power_value == POWER_0){
            A_PWM_CLOSE;
            motor_speed_pwm_set(A_PWM_CHANNEL,1);
        }else{
            motor_speed_pwm_set(A_PWM_CHANNEL, 45 + power_value * 2);
        } 
        if(power_value <= POWER_4){
            A_PWM_AC_L;
        }else{
            A_PWM_AC_H;
        }
    }else{
     
        if(power_value == POWER_0){
            A_PWM_CLOSE;
            motor_speed_pwm_set(A_PWM_CHANNEL,1);
        }else if(power_value < POWER_2){
            A_PWM_DC_OPEN_L;
            motor_speed_pwm_set(A_PWM_CHANNEL, 45 + POWER_4 * 2);
        }else{
            A_PWM_DC_OPEN_H;
            motor_speed_pwm_set(A_PWM_CHANNEL, 45 + POWER_8 * 2);
        }

    }

}


/* 工作模式暂停 */
void magnetic_pause_or_stop(uint8_t status)
{
    BaseType_t err = pdFALSE;
    err = xSemaphoreTake(xSemaphore_AC,0);
    if(err == pdPASS){
        mygptimer2_stop();
    }
    if(status == COIL_MOTOR_STOP){
        motor_speed_pwm_set(A_PWM_CHANNEL,A_PWM_DUTY_CLOSE);
    }
    
    A_PWM_CLOSE;
}



static uint16_t flagqq = 0;
void magnetic_pin_toggle(uint8_t mode, uint8_t temp_protect)
{
    if(mode == MODE_AC){
        if(temp_protect == TEMP_PROTECT_CLOSE){
            /* 正常运行 */
            switch(flagqq){
                case 0:
                gpio_set_level(GPIO_A_2_PORT,0);
                gpio_set_level(GPIO_A_1_PORT,1);
                
                flagqq = 1;
                    break;
                case 1:
                gpio_set_level(GPIO_A_2_PORT,0);
                gpio_set_level(GPIO_A_1_PORT,1);
                
                flagqq = 2;
                    break;
        
                case 2:
                gpio_set_level(GPIO_A_1_PORT,0);
                gpio_set_level(GPIO_A_2_PORT,1);
                flagqq = 3;
                    break;
        
                case 3:
                gpio_set_level(GPIO_A_1_PORT,0);
                gpio_set_level(GPIO_A_2_PORT,1);
                
                flagqq = 0;
                    break;
                default:
                    flagqq = 0;
                    break;
            }
        }else{
            /* 开启温度保护 */
            switch(flagqq){
                case 0:
                gpio_set_level(GPIO_A_1_PORT,0);
                gpio_set_level(GPIO_A_2_PORT,1);
                flagqq = 1;
                    break;
                case 1:
                gpio_set_level(GPIO_A_1_PORT,0);
                gpio_set_level(GPIO_A_2_PORT,0);
                flagqq = 2;
                    break;
        
                case 2:
                gpio_set_level(GPIO_A_1_PORT,0);
                gpio_set_level(GPIO_A_2_PORT,0);
                flagqq = 3;
                    break;
        
                case 3:
                gpio_set_level(GPIO_A_2_PORT,0);
                gpio_set_level(GPIO_A_1_PORT,1);
                flagqq = 0;
                    break;
                default:
                flagqq = 0;
                    break;
            }
        }

    }else{
        /* DC */
        if(temp_protect == TEMP_PROTECT_CLOSE){
            if(flagqq <= 19){
                gpio_set_level(GPIO_A_1_PORT,1);
                gpio_set_level(GPIO_A_2_PORT,0);
            }else{
                gpio_set_level(GPIO_A_1_PORT,0);
                gpio_set_level(GPIO_A_2_PORT,0);
            }
    
            if(flagqq > 20){
                flagqq = 0;
            }
            flagqq++;

        }else{
            if(flagqq <= 4){
                gpio_set_level(GPIO_A_1_PORT,1);
                gpio_set_level(GPIO_A_2_PORT,0);
            }else{
                gpio_set_level(GPIO_A_1_PORT,0);
                gpio_set_level(GPIO_A_2_PORT,0);
            }
    
            if(flagqq > 20){
                flagqq = 0;
            }
            flagqq++;
        }

    }



    // if(flagqq == 0){

    //     gpio_set_level(GPIO_A_1_PORT,0);
    //     gpio_set_level(GPIO_A_2_PORT,1);

    // }else{
    //     gpio_set_level(GPIO_A_2_PORT,0);
    //     gpio_set_level(GPIO_A_1_PORT,1);
    // }
    // flagqq = !flagqq;
}





